package chap05;

/**
 * Created by root on 2017/7/7.
 */
class Link1{
    public long dData;
    public Link1 next;

    public Link1(long d){
        dData = d;
    }

    public void displayLink(){
        System.out.print(dData + " ");
    }
}
class FirstLastList{
    private Link1 first;
    private Link1 last;

    public boolean isEmpty(){
        return first==null;
    }

    public void insertFirst(long d){
        Link1 newLink = new Link1(d);
        if (isEmpty()) {
            last = newLink;
        }
        newLink.next = first;
        first = newLink;
    }

    public void insertLast(long d){
        Link1 newLink = new Link1(d);
        if (isEmpty()) {
            first = newLink;
        } else {
            last.next = newLink;
        }
        last = newLink;
    }
    public Link1 deleteFirst(){
        Link1 theLink = first;
        if (first.next == null) {
            last = null;
        }
        first = first.next;
        return theLink;
    }

    public void displayList() {
        System.out.print("List (first-->last):　");
        Link1 current = first;
        while (current!=null) {
            current.displayLink();
            current = current.next;
        }
        System.out.println();
    }
}
public class FirstLastListApp {
    public static void main(String[] args){
        FirstLastList theList = new FirstLastList();

        theList.insertFirst(22);
        theList.insertFirst(44);
        theList.insertFirst(66);

        theList.insertLast(11);
        theList.insertLast(33);
        theList.insertLast(55);

        theList.displayList();

        theList.deleteFirst();
        theList.deleteFirst();
        theList.displayList();

    }
}
